package com.qbw.app.module.user.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.http.okhttp.HttpTask;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.UserActivityRegistBinding;
import com.qbw.app.model.Country;
import com.qbw.app.model.VerifyCode;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.util.CountryUtil;
import com.qbw.app.module.user.fragment.UserRegistAfterVerifyCodeFragment;
import com.qbw.app.module.user.fragment.UserRegistPhoneFragment;
import com.qbw.app.module.user.fragment.UserRegistVerifyCodeFragment;
import com.qbw.app.request.impl.CommonRequest;
import com.qbw.core.observable.ObservableString;
import com.qbw.spm.P;

import static com.qbw.app.Constant.KEY_SELECTED_COUNTRY;

/**
 * @author QBW
 * @date 2019/9/16
 */
public class UserRegistActivity extends CommonActivity<UserActivityRegistBinding> {

    private VerifyCode mVerifyCode = new VerifyCode();
    public static final int FRAGMENT_PHONE = 1;
    public static final int FRAGMENT_VERIFYCODE = 2;
    public static final int FRAGMENT_AFTER_VERIFYCODE = 3;
    private Country mSelectedCountry;
    public static final int REQUEST_CODE_SELECT_COUNTRY_AREA = 1;
    private ObservableString mCountryArea = new ObservableString();
    private ObservableString mPhone = new ObservableString();

    public static void start(Context context) {
        Intent intent = new Intent(context, UserRegistActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.user_activity_regist;
    }

    @Override
    protected int getContainerViewId() {
        return R.id.container;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding.title.setListener(this);
        int cid = CountryUtil.getCountryId();
        Country.Response response = Country.Response.fromJson(P.getString(Constant.KEY_ALL_COUNTRYS));
        if (response != null) {
            mSelectedCountry = response.getCountry(cid);
            if (mSelectedCountry == null) {
                if (response.size() > 0) {
                    mSelectedCountry = response.getData().get(0);
                    mCountryArea.setValue(mSelectedCountry.getTelCode());
                }
            } else {
                mCountryArea.setValue(mSelectedCountry.getTelCode());
            }
        }
        addFragment(FRAGMENT_PHONE, UserRegistPhoneFragment.createInstance());
        addFragment(FRAGMENT_VERIFYCODE, UserRegistVerifyCodeFragment.createInstance());
        addFragment(FRAGMENT_AFTER_VERIFYCODE, UserRegistAfterVerifyCodeFragment.createInstance());
        switchFragment(FRAGMENT_PHONE);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_SELECT_COUNTRY_AREA) {
                Country country = (Country) data.getSerializableExtra(KEY_SELECTED_COUNTRY);
                mCountryArea.setValue(country.getTelCode());
                mSelectedCountry = country;
            }
        }
    }

    public Country getSelectedCountry() {
        return mSelectedCountry;
    }

    public ObservableString getCountryArea() {
        return mCountryArea;
    }

    public ObservableString getPhone() {
        return mPhone;
    }

    public VerifyCode getVerifyCode() {
        return mVerifyCode;
    }
}
